
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GLES/gl.h> /* For NDK*/
#include <android/log.h>
#include "utils.h"
#include "jade.h"



///////////////////////////////////////////////////////////////////////////////
/// Jade model API


void jade_model_set_mesh_count (jade_model_t *model, int meshCount) {

    int i = 0;

    if (model->m_meshes != NULL) {
        for (; i < model->m_meshCount; i++)
            jade_mesh_destroy (model->m_meshes[i]);

        FREEANDNULL (model->m_meshes);
    }    


    if (meshCount > 0) {
        
        model->m_meshCount = meshCount; 
        model->m_meshes = (jade_mesh_t**)malloc (sizeof (jade_mesh_t*) * meshCount);
        for (i = 0; i < meshCount; i++) {
            model->m_meshes[i] = jade_mesh_create ();
            //model->m_meshes[i] = (jade_mesh_t *)malloc (sizeof (jade_mesh_t));
        }

    }   

}



jade_model_t *jade_model_create (int mesh_count) {

    jade_model_t *model = NULL;


    model = (jade_model_t*)malloc (sizeof (jade_model_t));
    if (model == NULL)
        return NULL;

    model->m_meshes = NULL;
    model->m_meshCount = 0;
 
    if (mesh_count > 0)
        jade_model_set_mesh_count (model, mesh_count);

    return model;
}

void jade_model_destroy (jade_model_t *model) {

    int i = 0;


    if (model->m_meshCount > 0) {

        for (; i < model->m_meshCount; i++)
            jade_mesh_destroy (model->m_meshes[i]);
    
        FREEANDNULL (model->m_meshes);
        FREEANDNULL (model);
    }
}

void 
jade_model_set_vertices (jade_model_t *model, GLfloat *vertices, int size, int meshIndex) {

 
    jade_mesh_set_vertices(model->m_meshes[meshIndex], vertices, size);

    return;
}

void 
jade_model_set_normals (jade_model_t *model, GLfloat *normals, int size, int meshIndex) {

    jade_mesh_set_normals (model->m_meshes[meshIndex], normals, size);
}

void 
jade_model_set_uvs (jade_model_t *model, GLfloat *uvs, int size, int meshIndex) {

    jade_mesh_set_uvs (model->m_meshes[meshIndex], uvs, size);
}

void 
jade_model_set_colors(jade_model_t *model, GLubyte *colors, int size, int meshIndex) {

    jade_mesh_set_colors(model->m_meshes[meshIndex], colors, size);
}

void
jade_model_set_indices (jade_model_t *model, GLshort *indices, int size, int meshIndex) {

    jade_mesh_set_indices (model->m_meshes[meshIndex], indices, size);
}

void 
jade_model_set_triangle_num(jade_model_t *model, GLint triangleNums, int meshIndex) {

    jade_mesh_set_triangle_num(model->m_meshes[meshIndex], triangleNums);
}


void jade_model_render (jade_model_t *model) {

    int i = 0;

    for (i = 0; i < model->m_meshCount; i++) {
        jade_mesh_render (model->m_meshes[i]);
    }

    return;
}




